package com.yoomiito.app.ui.my;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.yoomiito.app.Constant;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.base.WebViewActivity;
import com.yoomiito.app.net.YmApi;
import com.yoomiito.app.ui.my.psw.PswManagerActivity;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.CacheUtils;
import com.yoomiito.app.utils.EventMessage;
import com.yoomiito.app.utils.L;
import com.yoomiito.app.utils.LoginUtils;
import com.yoomiito.app.utils.NetworkUtils;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.ui.push.UMManager;
import com.yoomiito.app.utils.UserInfoConfig;
import com.yoomiito.app.utils.video.VideoUtils;
import com.yoomiito.app.widget.dialog.AccountCancelDialog;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.cache.SharedPref;
import cn.droidlover.xdroidmvp.net.ApiException;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.BaseModel;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.router.Router;

public class SetActivity extends BaseActivity<SetPresenter> {

    @BindView(R.id.tv_right)
    TextView rightTitleTv;
    @BindView(R.id.tv_center)
    TextView titleTv;
    @BindView(R.id.tv_exit)
    Button txExit;


    @Override
    public int getLayoutId() {
        return R.layout.act_set;
    }

    @Override
    public SetPresenter newP() {
        return null;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        rightTitleTv.setVisibility(View.VISIBLE);
        titleTv.setVisibility(View.VISIBLE);
        titleTv.setText("设置");
        rightTitleTv.setText("注销账号");
        rightTitleTv.setTextColor(ResUtil.getColor(R.color.color_FF035B));
    }

    @OnClick({R.id.tv_exit,R.id.iv_back_left,R.id.tv_right,R.id.act_set_psw_manager,R.id.act_set_about_us})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.iv_back_left:
                finish();
                break;
            case R.id.tv_right:
                AccountCancelDialog accountCancelDialog = new AccountCancelDialog(this, new AccountCancelDialog.OnCloseListener() {
                    @Override
                    public void confirm() {
                        accountCancel();
                    }
                });
                accountCancelDialog.show();
                break;
            case R.id.tv_exit:
                loginOut();
                break;
            case R.id.act_set_psw_manager:
//                if (UserInfoConfig.havePayPsw()){
                    PswManagerActivity.start(this);
//                }else {
//                    SetPayPswActivity.start(this);
//                }
                break;
            case R.id.act_set_about_us:
                Router.newIntent(this)
                        .to(WebViewActivity.class)
                        .putInt(Constant.TYPE,AllCode.AboutUs)
                        .launch();
                break;

        }
    }

    private void loginOut() {
        if (NetworkUtils.checkNetPermission(this)){
            return;
        }
        YmApi.getYmService()
                .logout()
                .compose(bindToLifecycle())
                .compose(XApi.getApiTransformerNoData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<BaseModel>() {
                    @Override
                    public void onSuccess(BaseModel baseModel) {
                        LoginUtils.INSTANCE.loginOut();
                        finish();
                    }

                    @Override
                    public void onFail(ApiException error) {
                        handleError(error);
                    }
                });
    }


    private void accountCancel() {
        if (NetworkUtils.checkNetPermission(this)){
            return;
        }
        YmApi.getYmService()
                .accountCancel()
                .compose(bindToLifecycle())
                .compose(XApi.getApiTransformerNoData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<BaseModel>() {
                    @Override
                    public void onSuccess(BaseModel baseModel) {
                        LoginUtils.INSTANCE.loginOut();
                        finish();
                    }

                    @Override
                    public void onFail(ApiException error) {
                        handleError(error);
                    }
                });
    }
}
